﻿using System.Web.Mvc;

namespace dPet.Web.Hospital.Controllers
{
	using dPet.Application.DataObjects;
	using dPet.Application.QueryObjects;
	using dPet.Application.Services;
	using dPet.Web.Hospital.Fliters;
	using dPet.Web.Hospital.ViewModels;
	using Hangerd.Mvc;
	using Hangerd.Mvc.ViewModels;

	public class SaleOrderController : HangerdController
	{
		private const int SALE_ORDER_LIST_PAGE_SIZE = 15;

		private readonly ISalesService _salesService;
		private readonly ITemplateService _templateService;

		public SaleOrderController(
			ISalesService salesService,
			ITemplateService templateService)
		{
			_salesService = salesService;
			_templateService = templateService;
		}

		[StaffLoginAuth(Privilege = PrivilegesDto.SaleOrderManagement)]
		public ActionResult Create()
		{
			return View();
		}

		[HttpPost]
		[StaffLoginAuth(Privilege = PrivilegesDto.SaleOrderManagement, Ajax = true, DefaultAjaxResult = "{\"Success\":false,\"Message\":\"操作失败\"}")]
		public ActionResult Create(ShoppingCartItemDto[] items, MembershipLevelDto membershipLevel = MembershipLevelDto.None)
		{
			var loginStaff = LoginStaffModel.Current;
			var result = _salesService.CreateSaleOrder(loginStaff.HospitalId, membershipLevel, items, loginStaff);

			return JsonContent(new
			{
				Success = result.Value != null,
				Message = result.Message,
				OrderId = result.Value != null ? result.Value.Id : string.Empty
			});
		}

		[StaffLoginAuth(Privilege = PrivilegesDto.SaleOrderManagement)]
		public ActionResult Pay(string id)
		{
			var order = _salesService.GetSaleOrder(id, OrderStatusDto.Init);

			if (order == null)
			{
				return RedirectToAction("List", "SaleOrder");
			}

			return View(order);
		}

		[HttpPost]
		[StaffLoginAuth(Privilege = PrivilegesDto.SaleOrderManagement)]
		public ActionResult Pay(string orderId, PaymentMethodDto paymentMethod = PaymentMethodDto.Cash)
		{
			var result = _salesService.PaySaleOrder(orderId, paymentMethod, LoginStaffModel.Current);

			return base.AlertAndRedirect(result.Message, result.Value
				? string.Format("/SaleOrder/List?status={0}", OrderStatusDto.Completed)
				: string.Format("/SaleOrder/Pay/{0}", orderId));
		}

		[HttpPost]
		[StaffLoginAuth(Privilege = PrivilegesDto.SaleOrderManagement)]
		public ActionResult Cancel(string orderId)
		{
			var loginStaff = LoginStaffModel.Current;
			var result = _salesService.CancelSaleOrder(orderId, loginStaff);

			return base.AlertAndRedirect(result.Message, result.Value
				? string.Format("/SaleOrder/List?status={0}", OrderStatusDto.Canceled)
				: string.Format("/SaleOrder/Pay/{0}", orderId));
		}

		[StaffLoginAuth(Privilege = PrivilegesDto.SaleOrderManagement)]
		public ActionResult List(SaleOrderQueryDto queryDto, int pageIndex = 1)
		{
			var page = pageIndex < 1 ? 1 : pageIndex;
			var totalNumber = 0;
			var orders = _salesService.GetSaleOrders(LoginStaffModel.Current.HospitalId, queryDto, page, SALE_ORDER_LIST_PAGE_SIZE, out totalNumber);

			ViewBag.QueryDto = queryDto;

			return View(new PagedListModel<SaleOrderDto>
			{
				PageIndex = page,
				PageSize = SALE_ORDER_LIST_PAGE_SIZE,
				TotalNumber = totalNumber,
				List = orders
			});
		}

		[StaffLoginAuth(Privilege = PrivilegesDto.SaleOrderManagement)]
		public ActionResult Bill(string id)
		{
			var saleOrder = _salesService.GetSaleOrder(id);

			if (saleOrder == null)
			{
				return RedirectToAction("List", "SaleOrder");
			}

			return View(new SaleOrderBillModel
			{
				Template = _templateService.GetBillTemplate(LoginStaffModel.Current.HospitalId) ?? new BillTemplateDto(),
				SaleOrder = saleOrder
			});
		}
	}
}
